﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Text.RegularExpressions;
using System.Xml;

namespace Transformer.iHunter.Plugins.BBS
{
    public partial class BbsChannelConfigureControl : UserControl, System.Xml.Serialization.IXmlSerializable
    {
        public BbsChannelConfigureControl()
        {
            InitializeComponent();
            
        }

        private bool IsConfigureValid(out string remark)
        {
            remark = "";
                       

            if (textBox_Address.Text == null || textBox_Address.Text.Trim() == "")
            {
                remark = "Please input BBS address";
                return false;
            }

            if (comboBox_Type.SelectedItem == null)
            {
                remark = "Please select BBS type";
                return false;
            }

            if (textBox_Username.Text == null || textBox_Username.Text.Trim() == "")
            {
                remark = "Please input your username";
                return false;
            }

            /*if ((textBox_Password.Text == null || textBox_Password.Text == string.Empty)
                && "guest" != textBox_Username.Text)
            {
                remark = "Only id 'guest' does not need password";
                return false;
            }*/

            if (textBox_Password.Text == null || textBox_Password.Text == string.Empty)
            {
                remark = "Password can not be empty";
                return false;
            }
			
            return true;
        }

        /*private void button_OK_Click(object sender, EventArgs e)
        {
            string reason;
            if (!IsConfigureValid(out reason))
            {
                MessageBox.Show(reason);
                return;
            }

			string str = this.GetConfigure();

			XmlDocument doc = new XmlDocument();
			doc.LoadXml(str);
			doc.Save("test.xml");

           // this.DialogResult = DialogResult.OK;
            this.Visible = false;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            //this.DialogResult = DialogResult.Cancel;
            this.Visible = false;
        }*/

		public XmlDocument GetConfigure()
		{
			XmlDocument doc = new XmlDocument();
			XmlElement root = doc.CreateElement("BbsChannelSettings");
			doc.AppendChild(root);
            			
			XmlElement ele = doc.CreateElement("Address");
			ele.InnerText = textBox_Address.Text.Trim();
			root.AppendChild(ele);

			ele = doc.CreateElement("Port");
			ele.InnerText = numericUpDown_Port.Value.ToString();
			root.AppendChild(ele);

			ele = doc.CreateElement("SSH");
			ele.InnerText = checkBox_SSH.Checked.ToString();
			root.AppendChild(ele);

			ele = doc.CreateElement("Type");
			ele.InnerText = comboBox_Type.SelectedItem.ToString();
			root.AppendChild(ele);

			ele = doc.CreateElement("Username");
			ele.InnerText = textBox_Username.Text.Trim();
			root.AppendChild(ele);

			ele = doc.CreateElement("Password");
			string pwd = textBox_Password.Text;
			if (null == pwd)
				pwd = "";
			ele.InnerText = pwd;
			root.AppendChild(ele);

			ele = doc.CreateElement("Mailbox");
			ele.InnerText = checkBox_Mailbox.Checked.ToString();
			root.AppendChild(ele);

			ele = doc.CreateElement("Boards");
            string board = "";
            if (null != listBox_Boards.Items)
            {
                foreach (var b in listBox_Boards.Items)
                {
                    board += (b.ToString() + ";");
                }
            }
			
			ele.InnerText = board;
			root.AppendChild(ele);

			return doc;
		}

		public void SetCongifure(XmlNode root)
		{						
			try
			{			
				XmlElement ele = root["Address"];
				textBox_Address.Text = ele.InnerText;

				ele = root["Port"];
				numericUpDown_Port.Value = Decimal.Parse(ele.InnerText);

				ele = root["SSH"];
				checkBox_SSH.Checked = ("True" == ele.InnerText);

				ele = root["Type"];
				if ("Net9" == ele.InnerText)
					comboBox_Type.SelectedIndex = 1;
				else
					comboBox_Type.SelectedIndex = 0;

				ele = root["Username"];
				textBox_Username.Text = ele.InnerText;

				ele = root["Password"];
				textBox_Password.Text = ele.InnerText;

				ele = root["Mailbox"];
				checkBox_Mailbox.Checked = ("True" == ele.InnerText);

				ele = root["Boards"];
                listBox_Boards.Items.Clear();
                string boards = ele.InnerText;
                if (boards.Length > 0)
                {
                    System.Diagnostics.Debug.Assert(boards.EndsWith(";"));
                    boards.Remove(boards.Length - 1);

                    string[] names = boards.Split(
                        new char[]{';'},
                        StringSplitOptions.RemoveEmptyEntries
                        );

                    foreach (string boardname in names)
                    {
                        listBox_Boards.Items.Add(boardname.Trim());
                    }
                }
			}
			catch {}
		}

		#region IXmlSerializable Members

		public System.Xml.Schema.XmlSchema GetSchema()
		{
			return null;
		}

		public void ReadXml(XmlReader reader)
		{
			XmlDocument doc = new XmlDocument();
            doc.Load(reader);
			SetCongifure(doc.DocumentElement);
		}

		public void WriteXml(XmlWriter writer)
		{
			string reason;
			if (!IsConfigureValid(out reason))
			{
				MessageBox.Show(reason);

				throw new ArgumentException(reason);
			}

			XmlDocument doc = GetConfigure();
			doc.WriteTo(writer);
		}

		#endregion

        private void button_Add_Click(object sender, EventArgs e)
        {
            BbsBoardConfigDialog boardDialog = new BbsBoardConfigDialog();
            if (boardDialog.ShowDialog() == DialogResult.OK)
            {
                List<string> boards = boardDialog.Boards;
                foreach (string board in boards)
                {
                    listBox_Boards.Items.Add(board);
                }
            }
        }

        private void listBox_Boards_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool selected = listBox_Boards.SelectedIndex >= 0;

            button_Edit.Enabled = button_Delete.Enabled = selected;
        }

        private void button_Edit_Click(object sender, EventArgs e)
        {
            BbsBoardConfigDialog boardDialog = new BbsBoardConfigDialog();
            string original = listBox_Boards.Items[listBox_Boards.SelectedIndex].ToString();
            if (null == original)
                original = string.Empty;
            boardDialog.SetBoard(original);            
            if (boardDialog.ShowDialog() == DialogResult.OK)            
            {
                listBox_Boards.Items.RemoveAt(listBox_Boards.SelectedIndex);

                List<string> boards = boardDialog.Boards;
                foreach (string board in boards)
                {
                    listBox_Boards.Items.Add(board);
                }
            }
        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            listBox_Boards.Items.RemoveAt(listBox_Boards.SelectedIndex);
        }
	}
}
